let app = getApp();
Page({
  data: {
    ossImg: `${app.globalData.imgUrl}/applet_web_images/new_image`,
    imgUrl: app.globalData.imgUrl,
    order_id: '',
    attr_id: '',
    goods_id: '',
    goods_info: {}, // 产品信息
    rent_and_give: {}, // 租赁送价
    rent_and_return: {}, // 租完归还
    total_save_money: '', // 省钱
    packageShow: true, // 套餐隐藏
    shadowAnimation: [], // 动画背景
    contentAnimation: [], // 动画内容
    isShowChangePopup: false, // 是否展示替换弹窗
  },

  changePackage() {
    var app = new getApp();
    const data = {
      zm_user_id: app.globalData.user_id,
      order_id: this.data.order_id,
    }
    my.request({
      url: app.globalData.testUrl + '/api/user/applyChangeLeasingSchemes',
      method: 'POST',
      data,
      dataType: 'json',
      success: ({data: {code}}) => {
        if(code !== 1) {
          my.showToast({
            type: 'fail',
            content: '更换套餐失败',
            duration: 3000,
          });
          return
        }
        this.setData({
          packageShow: !this.data.packageShow
        })
        this.showShadow()
        this.showContent()
      }
    })
  },

  // 显示弹窗层背景
  showShadow() {
    let animation = my.createAnimation({
      duration: 200,
      timingFunction: 'cubic-bezier(.55, 0, .55, .2)',
    })
    this.shadowAnim = animation
    animation.opacity(1).step()
    this.setData({
      shadowAnimation: animation.export()
    })
    my.hideLoading()
  },

  // 显示弹出层面板
  showContent() {
    var animation = my.createAnimation({
      duration: 200,
      timingFunction: 'cubic-bezier(.55, 0, .55, .2)',
    });
    this.contentAnim = animation;
    animation.translateY(0).step();
    this.setData({
      contentAnimation: animation.export(),
    });
  },


  // 隐藏弹窗层背景
  hideShadow() {
    this.shadowAnim.opacity(0).step();
    this.setData({
      shadowAnimation: this.shadowAnim.export(),
    });
  },

  // 隐藏弹出层内容
  hideContent() {
    this.contentAnim.translateY('100%').step();
    this.setData({
      contentAnimation: this.contentAnim.export(),
    });
  },
  // 关闭弹出层
  hidePanel() {
    this.hideShadow()
    this.hideContent()
    this.setData({
      packageShow: true
    })
    my.redirectTo({
      url: `../../myorders/orderdetail/orderdetail?order_id=${this.data.order_id}`
    })

  },
  // 更换套餐
  onLoad({order_id, attr_id, goods_id, platform}) {
    
    this.setData({
      order_id,
      attr_id,
      goods_id
    })

    if(platform) {
      app.globalData.platform = platform
    }
    this.initPackageData()
  },

  // 初始化套餐数据
  initPackageData() {
    const {
      goods_id,
      attr_id
    } = this.data
    my.request({
      url: `${app.globalData.testUrl}/api/goods/getGoodsDifferentLeaseSchemeRentInfo`,
      method: 'POST',
      data: {
        goods_id,
        attr_id
      },
      dataType: 'json',
      success: ({data: {data, code, msg}}) => {
        if(code !== 1) {
          my.showToast({
            type: 'fail',
            content: msg,
            duration: 3000,
          });
          return
        }
        const {
          goods_info, rent_and_give, rent_and_return, total_save_money
        } = data

        this.setData({
          goods_info, rent_and_give, rent_and_return, total_save_money
        })
      }
    })
  },
  confirmCancel() {
    this.setData({
      isShowChangePopup: false
    })
    my.navigateBack();
  },
  noCancel() {
    this.setData({
      isShowChangePopup: false
    })
  },
  notYetChange() {
    this.setData({
      isShowChangePopup: true
    })
 
  }
})